package com.wzt.lianfirecontrol.fragment.jinji;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.activity.ShowFragActivity;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.function.jinjiweixiu.JingJiWeiXiuModel;
import com.wzt.lianfirecontrol.fragment.BaseFragment;
import com.wzt.lianfirecontrol.utils.GildeUtils;
import com.wzt.lianfirecontrol.utils.PreferencesUtils;
import com.wzt.lianfirecontrol.utils.ScreenUtils;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.Utils;
import com.wzt.lianfirecontrol.view.InitView;

public class JinJiWeiXiuDetailFragment extends BaseFragment {
    private BaseActivity activity;
    private Bundle bundle;
    private View itemContentView;

    private LinearLayout ll_title;
    private TextView tv_title;
    private TextView tv_status;
    private TextView tv_device_location;
    private TextView tv_person_name;
    private TextView tv_person_phone;
    private TextView tv_error_time;
    private TextView tv_error_content;

    private LinearLayout ll_device_three_photo;
    private View include_play_photo_three;
    private RelativeLayout rl_device_photo_1;
    private ImageView iv_device_photo_1;
    private ImageView iv_photo_delete_1;
    private RelativeLayout rl_device_photo_2;
    private ImageView iv_device_photo_2;
    private ImageView iv_photo_delete_2;
    private RelativeLayout rl_device_photo_3;
    private ImageView iv_device_photo_3;
    private ImageView iv_photo_delete_3;

    private View include_no_data;
    private View include_no_net;
    private View include_progress_bar;

    private JingJiWeiXiuModel jingJiWeiXiuModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == itemContentView){
            activity = getMyActivity();
            bundle = getArguments();
            if (null != bundle){
                jingJiWeiXiuModel = (JingJiWeiXiuModel) bundle.getSerializable("data");
            }
            if (null == jingJiWeiXiuModel){
                activity.finish();
            }
            PreferencesUtils.putString(activity, ConstData.CHANGESTATUS, "-1");
            itemContentView = inflater.inflate(R.layout.f_jinji_detail, container, false);
            initView();
            setDetailData();
        }
        if (itemContentView.getParent() != null) {
            ((ViewGroup) itemContentView.getParent()).removeView(itemContentView);
        }
        return itemContentView;
    }

    private void initView() {

        ll_title = itemContentView.findViewById(R.id.ll_title);
        tv_title = itemContentView.findViewById(R.id.tv_title);
        tv_status = itemContentView.findViewById(R.id.tv_status);
        tv_device_location = itemContentView.findViewById(R.id.tv_device_location);
        tv_person_name = itemContentView.findViewById(R.id.tv_person_name);
        tv_person_phone = itemContentView.findViewById(R.id.tv_person_phone);
        tv_error_time = itemContentView.findViewById(R.id.tv_error_time);
        tv_error_content = itemContentView.findViewById(R.id.tv_error_content);

        initPhotoThree();

        include_no_data = itemContentView.findViewById(R.id.include_no_data);
        include_progress_bar = itemContentView.findViewById(R.id.include_progress_bar);
        InitView.instance().initProgressBar(activity, include_progress_bar);
        include_no_net = itemContentView.findViewById(R.id.include_no_net);
        include_no_net.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateData();
            }
        });
    }

    private void initPhotoThree() {
        ll_device_three_photo = itemContentView.findViewById(R.id.ll_device_three_photo);
        include_play_photo_three = itemContentView.findViewById(R.id.include_play_photo_three);
        rl_device_photo_1 = itemContentView.findViewById(R.id.rl_device_photo_1);
        iv_device_photo_1 = itemContentView.findViewById(R.id.iv_device_photo_1);
        iv_photo_delete_1 = itemContentView.findViewById(R.id.iv_photo_delete_1);
        iv_photo_delete_1.setVisibility(View.GONE);
        rl_device_photo_2 = itemContentView.findViewById(R.id.rl_device_photo_2);
        iv_device_photo_2 = itemContentView.findViewById(R.id.iv_device_photo_2);
        iv_photo_delete_2 = itemContentView.findViewById(R.id.iv_photo_delete_2);
        iv_photo_delete_2.setVisibility(View.GONE);
        rl_device_photo_3 = itemContentView.findViewById(R.id.rl_device_photo_3);
        iv_device_photo_3 = itemContentView.findViewById(R.id.iv_device_photo_3);
        iv_photo_delete_3 = itemContentView.findViewById(R.id.iv_photo_delete_3);
        iv_photo_delete_3.setVisibility(View.GONE);

        int width = (ScreenUtils.getScreenWidth(activity) - Utils.dip2px(activity, 90)) / 3;
        rl_device_photo_1.getLayoutParams().width = width;
        rl_device_photo_1.getLayoutParams().height = width;
        rl_device_photo_2.getLayoutParams().width = width;
        rl_device_photo_2.getLayoutParams().height = width;
        rl_device_photo_3.getLayoutParams().width = width;
        rl_device_photo_3.getLayoutParams().height = width;

    }

    private void setDetailData() {
        if (null == jingJiWeiXiuModel){
            include_no_data.setVisibility(View.VISIBLE);
            return;
        }
        include_no_data.setVisibility(View.GONE);
        StringBuilder stringBuilder = new StringBuilder();
        if (!StringUtils.isEmpty(jingJiWeiXiuModel.getBuildingName())){
            stringBuilder.append(jingJiWeiXiuModel.getBuildingName());
        }
        if (!StringUtils.isEmpty(jingJiWeiXiuModel.getFloorName())){
            stringBuilder.append(jingJiWeiXiuModel.getFloorName());
        }
        if (!StringUtils.isEmpty(jingJiWeiXiuModel.getAddress())){
            stringBuilder.append(jingJiWeiXiuModel.getAddress());
        }
        if (StringUtils.isEmpty(stringBuilder.toString())){
            tv_device_location.setVisibility(View.GONE);
        }else {
            tv_device_location.setText(stringBuilder.toString());
        }
        tv_person_name.setText(jingJiWeiXiuModel.getRealName());
        tv_person_phone.setText(jingJiWeiXiuModel.getPhone());
        tv_error_time.setText(jingJiWeiXiuModel.getReportTime());
        tv_error_content.setText(jingJiWeiXiuModel.getRemark());

        String[] imgUrls = jingJiWeiXiuModel.getImgs().split(",");
        if (imgUrls.length >= 1 && imgUrls[0].startsWith("http")){
            GildeUtils.loadImageCorner(activity, iv_device_photo_1, imgUrls[0]);
            iv_device_photo_1.setVisibility(View.VISIBLE);
            iv_device_photo_1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((ShowFragActivity)activity).setShowImg(imgUrls[0]);
                }
            });
        }else {
            iv_device_photo_1.setVisibility(View.GONE);
        }

        if (imgUrls.length >= 2){
            GildeUtils.loadImageCorner(activity, iv_device_photo_2, imgUrls[1]);
            iv_device_photo_2.setVisibility(View.VISIBLE);
            iv_device_photo_2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((ShowFragActivity)activity).setShowImg(imgUrls[1]);
                }
            });
        }else {
            iv_device_photo_2.setVisibility(View.GONE);
        }

        if (imgUrls.length >= 3){
            GildeUtils.loadImageCorner(activity, iv_device_photo_3, imgUrls[2]);
            iv_device_photo_3.setVisibility(View.VISIBLE);
            iv_device_photo_3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((ShowFragActivity)activity).setShowImg(imgUrls[2]);
                }
            });
        }else {
            iv_device_photo_3.setVisibility(View.GONE);
        }

        if (StringUtils.isEmpty(jingJiWeiXiuModel.getEquName())){
            tv_title.setVisibility(View.INVISIBLE);
        }else {
            tv_title.setVisibility(View.VISIBLE);
            tv_title.setText(jingJiWeiXiuModel.getEquName());
        }

        if ("0".equals(jingJiWeiXiuModel.getStatus())){
            tv_status.setText("待处理");
            tv_status.setTextColor(activity.getResources().getColor(R.color.change_status_color));
        }else {
            tv_status.setText("已处理");
            tv_status.setTextColor(activity.getResources().getColor(R.color.remark_status_color));
        }
    }
}
